package com.zretc.support.controller;

import com.zretc.support.entity.User;
import com.zretc.support.service.UserService;
import com.zretc.support.tool.AjaxResponse;
import com.zretc.support.tool.PageData;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("api")
public class UserController {
    @Autowired
    UserService userService;

    //返回的类
    AjaxResponse ajaxResponse = new AjaxResponse();

    Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * 添加用户，因为id是字符串    ok
     * @param user
     * @return
     */
    @PostMapping(value = "addu",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "添加用户",notes = "用户添加用户")
    AjaxResponse addUser(@ApiParam(name = "user",value = "用户对象") @RequestBody User user){
        return userService.addUser(user);
    }

    /**
     * 更新用户，比如用户的信息，用户的权限，显示状态，ok
     * @param user
     * @return
     */
    @PostMapping(value = "updu",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "更新用户",notes = "可以更新用户的信息，用户的密码，用户的权限，显示状态")
    AjaxResponse updUser(@ApiParam(name = "user",value = "用户对象") @RequestBody User user){
        logger.info("user:"+user);
        return userService.updUser(user);
    }

    /**
     * 登录验证用户的用户名和密码，ok
     * @param username 用户名
     * @param userpwd 密码
     * @return
     */
    @PostMapping(value = "dologin",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "登录验证",notes = "用于用户名和密码的校验")
    AjaxResponse doLogin(String username, String userpwd,int userRole, HttpSession session){

        logger.info(username+","+userpwd);
        return userService.doLogin(username,userpwd,userRole,session);
    }

    /**
     * 模糊查询对象，有分页，ok
     * 这边的userrole必须给值，返回对应的列表
     * @param username  用户姓名
     * @param page    当前页码
     * @param limit  每页输出的条数
     * @return  返回值是用户列表
     */
    @GetMapping(value = "getulike")
    @ResponseBody
    public PageData<User> getlike(@RequestParam(value = "username",defaultValue = "") String username,
                                  @RequestParam(value = "userRole")int userRole,
                                  @RequestParam(value = "page",defaultValue = "1") int page,
                                  @RequestParam(value = "limit",defaultValue = "10") int limit){

        return userService.findByLike(username,userRole,(page-1),limit);
    }

    /**
     * 根据id查询用户
     * @param userId 用户id
     * @return
     */
    @GetMapping(value = "getu")
    public User getlike(String userId){

        return userService.findByUserId(userId);
    }

    /**
     * 输入的值，获取相应的信息
     * @param inputsearch
     * @param userRole
     * @param page
     * @param limit
     * @param select
     * @param selectsex
     * @return
     */
    @GetMapping(value = "getinput")
    @ResponseBody
    public PageData<User> getinput(@RequestParam(value = "inputsearch",defaultValue = "") String inputsearch,
                                   @RequestParam(value = "userRole")int userRole,
                                   @RequestParam(value = "page",defaultValue = "1") int page,
                                   @RequestParam(value = "limit",defaultValue = "10") int limit,
                                   @RequestParam(value = "select",defaultValue = "学生姓名") String select,
                                   @RequestParam(value = "selectsex",defaultValue = "性别不限") String selectsex){
        if (select.equals("学生姓名")){
            return userService.findBySelect(inputsearch,userRole,(page-1),limit,select,selectsex);
        }else {
            return userService.findByTeaSelect(inputsearch,userRole,(page-1),limit,select,selectsex);
        }

    }

    /**
     * 更新用户，比如用户的信息，用户的权限，显示状态，ok
     * @param user
     * @return
     */
    @PostMapping(value = "updpwd",produces = "application/json;charset=utf-8")
    @ApiOperation(value = "密码修改",notes = "可以更新用户的密码")
    AjaxResponse updUP(@ApiParam(name = "user",value = "用户对象") @RequestBody User user){
        logger.info("user:"+user);
        return userService.updPwd(user);
    }

    /**
     * 根据角色获取用户的编号
     * @param userRole
     * @return
     */
    @GetMapping(value = "getusers/{userRole}")
    public List<User> getusers(@PathVariable(value = "userRole")int userRole) {
        return userService.getusers(userRole);
    }
    /**
     * 根据用户编号获取姓名
     * @param userId
     * @return
     */
    @GetMapping(value = "getusername/{userId}")
    public String getusername(@PathVariable(value = "userId")String userId) {
        return userService.getusername(userId);
    }

}
